<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button
          size="mini"
          type="primary"
          @click="
            showDialog = true;
            isAdd = true;
          "
        >添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色" prop="name" />
        <el-table-column align="center" width="200" label="启用" prop="state">
          <!-- 自定义列表结构 -->
          <template v-slot="{ row }">
            {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="description" label="描述" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button size="mini" type="text">分配权限</el-button>
            <el-button size="mini" type="text" @click="showdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog width="500px" :title="isAdd ? '新增角色' : '编辑角色'" :visible.sync="showDialog" @close="btnCancel">
      <!-- 表单内容 -->
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名字" prop="name">
          <el-input v-model="roleForm.name" style="width: 300px" size="mini" />
        </el-form-item>
        <!-- 如果不做校验,而且不做数据清空,则不需要加prop属性 -->
        <el-form-item label="启用" prop="state">
          <el-switch v-model="roleForm.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width: 300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOK">确定</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, getRole, updateRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      total: 0, // 数据总条数
      pageParams: {
        page: 1, // 当前显示第几页
        pagesize: 5 // 每页多少条数据
      },
      showDialog: false, // 控制弹窗是否显示
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未启用 关闭 0 打开1
      },
      rules: {
        name: [
          { required: true, message: '角色名字不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      isAdd: true
    }
  },
  created() {
    // 数据初始化完成,调接口请求数据
    this.getRoleList()
  },
  methods: {
    // 调接口函数
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams)
      this.list = rows
      // 把总条数赋值
      this.total = total
    },
    // 当页码改变,执行该函数
    changePage(page) {
      // 修改要显示的页码
      this.pageParams.page = page
      // 重新调接口
      this.getRoleList()
    },
    btnOK() {
      this.$refs.roleForm.validate(async(isOK) => {
        if (isOK) {
          if (this.isAdd) {
            await addRole(this.roleForm)
            this.$message.success('新增角色成功!')
          } else {
            await updateRole(this.roleForm.id, this.roleForm)
            this.$message.success('修改角色成功!')
          }
          // 重新调接口
          this.getRoleList()
          // 关闭弹窗
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.$refs.roleForm.resetFields()
      this.roleForm = this.$options.data().roleForm
      this.showDialog = false
    },
    async showdit(id) {
      // 请求角色详情
      const res = await getRole(id)
      this.roleForm = res
      this.isAdd = false
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.role-operate {
  padding: 10px;
}
</style>
